package com.vercon.moensmart.adapter

import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.vercon.moensmart.MainActivity
import com.vercon.moensmart.R
import com.vercon.moensmart.adapter.base.BaseAdapter
import com.vercon.moensmart.databinding.ItemHomeViewDeviceListLayoutBinding
import com.vercon.moensmart.dialog.ShowerHeadControllerDialog
import com.vercon.moensmart.dialog.ToiletControllerDialog
import com.vercon.moensmart.dialog.TowelRackControllerDialog
import com.vercon.moensmart.mode.response.AllDevices
import com.vercon.moensmart.utils.FrescoUtil

class SmartDeviceListAdapter(context: Context?) : BaseAdapter<ItemHomeViewDeviceListLayoutBinding, AllDevices.ListDTO?>(context) {

    override fun baseViewHolder(parent: ViewGroup): BaseViewHolder {
        val binding: ItemHomeViewDeviceListLayoutBinding = ItemHomeViewDeviceListLayoutBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun bindItemView(viewHolder: BaseViewHolder?, position: Int, data: AllDevices.ListDTO?, total: Int) {
        viewHolder?.mBinding?.tvDeviceName?.text = data?.product_series
        viewHolder?.mBinding?.tvDeviceFa?.text = data?.device_alias
        if(data?.product_image.isNullOrEmpty()){
            if("ZMJTJP".equals(data?.product_id)){
                FrescoUtil.loadImage(viewHolder?.mBinding?.pictureImageview,Uri.parse("res://" + context?.packageName + "/" + R.mipmap.ic_towel_rack))
            }else if("2".equals(data?.product_id)){
                FrescoUtil.loadImage(viewHolder?.mBinding?.pictureImageview, Uri.parse("res://" + context?.packageName + "/" + R.mipmap.ic_showerhead))
            }else if("A87FI1".equals(data?.product_id)){
                FrescoUtil.loadImage(viewHolder?.mBinding?.pictureImageview, Uri.parse("res://" + context?.packageName + "/" + R.mipmap.ic_toilet))
            }
        }else{
            try {
                FrescoUtil.loadImage(viewHolder?.mBinding?.pictureImageview, Uri.parse(data?.product_image))
            }catch (e : Exception ){
                e.printStackTrace()
            }
        }


        if(data?.is_online == 1){
            viewHolder?.mBinding?.igOnlineState?.setBackgroundResource(R.mipmap.ic_device_online_1)
            viewHolder?.mBinding?.igOnlineStateText?.text = "在线"
            context?.resources?.getColor(com.vercon.common.R.color.color_45A1C9)
                ?.let { viewHolder?.mBinding?.igOnlineStateText?.setTextColor(it) }
        }else if(data?.is_online == 0){
            viewHolder?.mBinding?.igOnlineState?.setBackgroundResource(R.mipmap.ic_device_online_0)
            viewHolder?.mBinding?.igOnlineStateText?.text = "离线"
            context?.resources?.getColor(com.vercon.common.R.color.color_FE9149)
                ?.let { viewHolder?.mBinding?.igOnlineStateText?.setTextColor(it) }
        }else{
            viewHolder?.mBinding?.igOnlineState?.setBackgroundResource(0)
            viewHolder?.mBinding?.igOnlineStateText?.text = "敬请期待"
            context?.resources?.getColor(com.vercon.common.R.color.color_99B5BC)
                ?.let { viewHolder?.mBinding?.igOnlineStateText?.setTextColor(it) }
        }

//        viewHolder?.mBinding?.igDeviceDetails?.setOnClickListener {
//            mItemListener?.onItemClickListener(it,data,position)
//        }
        viewHolder?.itemView?.setOnClickListener {
            if("A87FI1".equals(data?.product_id)){
                val dialog = ToiletControllerDialog()
                var bundle = Bundle()
                bundle.putString(ToiletControllerDialog.DEVICE_ID,data?.device_id)
                bundle.putString(ToiletControllerDialog.PRODUCT_ID,data?.product_id)
                bundle.putString(ToiletControllerDialog.PRODUCT_PIC,data?.product_image)
                bundle.putString(ToiletControllerDialog.PRODUCT_NAME,data?.product_series)
                dialog.arguments = bundle
                dialog.show(MainActivity.Instance.supportFragmentManager,"")
            }else if("ZMJTJP".equals(data?.product_id)){
                val dialog = TowelRackControllerDialog()
                var bundle = Bundle()
                bundle.putString(TowelRackControllerDialog.DEVICE_ID,data?.device_id)
                bundle.putString(TowelRackControllerDialog.PRODUCT_ID,data?.product_id)
                bundle.putString(TowelRackControllerDialog.PRODUCT_PIC,data?.product_image)
                bundle.putString(TowelRackControllerDialog.PRODUCT_NAME,data?.product_series)
                dialog.arguments = bundle
                dialog.show(MainActivity.Instance.supportFragmentManager,"")
            }else if("2".equals(data?.product_id)){
                val dialog = ShowerHeadControllerDialog()
                var bundle = Bundle()
                bundle.putString(ShowerHeadControllerDialog.PRODUCT_PIC,data?.product_image)
                bundle.putString(ShowerHeadControllerDialog.PRODUCT_NAME,data?.product_series)
                dialog.arguments = bundle
                dialog.show(MainActivity.Instance.supportFragmentManager,"")
            }

        }

    }

    fun refreshDeviceState(is_online : Int,device_id : String){
        for ((i,bean) in list.withIndex()){
            if(bean?.device_id.equals(device_id)){
                bean?.is_online = is_online
                notifyDataSetChanged()
            }
        }
    }


    inner class ViewHolder(itemBinding: ItemHomeViewDeviceListLayoutBinding) : BaseViewHolder(itemBinding)
}